package com.honey.mybatis.generator.mappper;

import java.util.List;

import com.honey.compilation.java.JavaKeyWord;
import com.honey.compilation.xml.XmlElement;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.dbmapping.introspect.IntrospectTableColumn;
import com.honey.core.dbmapping.structure.Column;
import com.honey.core.dbmapping.structure.Schema;
import com.honey.core.dbmapping.structure.Table;
import com.honey.core.dbmapping.structure.TableColumn;
import com.honey.mybatis.generator.MybatisPlugin;

public class Basefield extends AbstractMybatisMapper{

	public Basefield(MybatisPlugin belongPlugin) {
		super(belongPlugin);
	}

	public void appendElement(XmlElement parent, Schema schema, IntrospectSchemaColumn introspectSchemaColumn ){
		parent.addNewLine();
		parent.addComments(
				"通用查询的字段"
		);
		
		XmlElement sql = new XmlElement(parent,"sql")
		.addAttribute("id","basefield")
		;
		
		if( schema instanceof Table ){
			IntrospectTableColumn introspectTableColumn = (IntrospectTableColumn)introspectSchemaColumn;
			List<Column> list =  introspectTableColumn.allColumns();
			 boolean comma = false;
			 StringBuilder sb = new StringBuilder();
			for(Column tc : list){
				TableColumn column = (TableColumn) tc;
				if( !column.isBLOBColumn() ){
					if (comma)sb.append(JavaKeyWord.OPERATION_COMMA); else comma = true;
					sb.append(column.getName()).append(" AS ").append( super.getFieldName(column));
				}
			}
			sql.addTextElement(sb.toString());
		}
	}
}
